<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2011 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  2012 Andreas Koob 
 * @author     Andreas Koob 
 * @package    piwik-analyser 
 * @license    LGPL 
 * @filesource
 */


/**
 * Class piwik_analyser_analyse
 *
 * @copyright  2012 Andreas Koob 
 * @author     Andreas Koob 
 * @package    Controller
 */
class piwik_analyser_analyse extends BackendModule
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'piwik_analyser';
	protected $functions;
	protected $currentModule;

	
	/**
	 * Generate module
	 */
	protected function compile()
	{
		$this->import('Database');
		$this->functions = new piwik_analyser_functions();
		$objAccounts=$this->Database->prepare("SELECT count(id) as cnt_acc FROM tl_piwik_analyser_accounts")->execute();
		if($objAccounts->cnt_acc==0)
		{
			//There is no account set up
			$this->Template = new BackendTemplate('piwik_analyser_noaccounts');
			$this->Template->headline='No account available';
			$this->Template->message='There is no piwik account set up. Please add an account in order to use the Piwik analyser.';
		}
		else
		{
			if($this->Input->post('submit')!='')
			{
				$this->log("Submit is [" . $this->Input->post('submit') . "]", 'piwik_analyser_analyse.compile()', TL_INFO);
				$this->log("Module is [" . $this->Input->post('module') . "]", 'piwik_analyser_analyse.compile()', TL_INFO);
				$this->log("Function is [" . $this->Input->post('function') . "]", 'piwik_analyser_analyse.compile()', TL_INFO);
				switch($this->Input->post('module'))
				{
					case 'api':
						$this->currentModule=new piwik_analyser_module_api();
						$this->Template->headline='Module API';
						break;
					case 'actions':
						$this->currentModule=new piwik_analyser_module_actions();
						$this->Template->headline='Module Actions';
						break;
					case 'customvariables':
						$this->currentModule=new piwik_analyser_module_customvariables();
						$this->Template->headline='Module Custom variables';
						break;
					case 'exampleapi':
						$this->currentModule=new piwik_analyser_module_exampleapi();
						$this->Template->headline='Module Example API';
						break;
					case 'goals':
						$this->currentModule=new piwik_analyser_module_goals();
						$this->Template->headline='Module Goals';
						break;
					case 'imagegraph':
						$this->currentModule=new piwik_analyser_module_imagegraph();
						$this->Template->headline='Module Image graph';
						break;
					case 'languagesmanager':
						$this->currentModule=new piwik_analyser_module_languagesmanager();
						$this->Template->headline='Module Languages manager';
						break;
					case 'live':
						$this->currentModule=new piwik_analyser_module_live();
						$this->Template->headline='Module Live';
						break;
					case 'multisites':
						$this->currentModule=new piwik_analyser_module_multisites();
						$this->Template->headline='Module Multi sites';
						break;
					case 'pdfreports':
						$this->currentModule=new piwik_analyser_module_pdfreports();
						$this->Template->headline='Module PDF reports';
						break;
					case 'provider':
						$this->currentModule=new piwik_analyser_module_provider();
						$this->Template->headline='Module Provider';
						break;
					case 'referers':
						$this->currentModule=new piwik_analyser_module_referers();
						$this->Template->headline='Module Referers';
						break;
					case 'seo':
						$this->currentModule=new piwik_analyser_module_seo();
						$this->Template->headline='Module SEO';
						break;
					case 'sitesmanager':
						$this->currentModule=new piwik_analyser_module_sitesmanager();
						$this->Template->headline='Module Sites manager';
						break;
					case 'usercountry':
						$this->currentModule=new piwik_analyser_module_usercountry();
						$this->Template->headline='Module User country';
						break;
					case 'usersettings':
						$this->currentModule=new piwik_analyser_module_usersettings();
						$this->Template->headline='Module User settings';
						break;
					case 'usersmanager':
						$this->currentModule=new piwik_analyser_module_usersmanager();
						$this->Template->headline='Module Users manager';
						break;
					case 'visitfrequency':
						$this->currentModule=new piwik_analyser_module_visitfrequency();
						$this->Template->headline='Module Visit frequency';
						break;
					case 'visittime':
						$this->currentModule=new piwik_analyser_module_visittime();
						$this->Template->headline='Module Visit time';
						break;
					case 'visitorinterest':
						$this->currentModule=new piwik_analyser_module_visitorinterest();
						$this->Template->headline='Module Visitor interest';
						break;
					case 'visitssummary':
						$this->currentModule=new piwik_analyser_module_visitssummary();
						$this->Template->headline='Module VisitsSummary';
						break;
				}//switch($this->Input->post('module'))
				$this->Template->functions=$this->currentModule->getFunctions($this->Input->post('function'));
				if($this->Input->post('function')!=''){//Function is selected
					$this->Template->queryCode=$this->currentModule->getQueryCode($this->Input->post('function'));
					$this->Template->selectedFunction=$this->Input->post('function');
					$this->Template->functionSelected=true;
				}
				if($this->Input->post('submit')=='Query')
				{
					$arrResult=$this->currentModule->processFunction($this->Input->post('function'));
				}
				$this->Template->result=$arrResult['result'];
				$this->Template->answer=$arrResult['answer'];
				$this->Template->query=$arrResult['query'];
				$this->Template->selectedModule=$this->Input->post('module');
				$this->Template->selectedAccount=$this->Input->post('account');
				$this->Template->moduleSelected=true;
				$this->Template->accounts=$this->functions->getAccountsCode($this->Input->post('account'));
			}//if($this->Input->post('submit')!='')
			else
			{
				$this->Template->headline='Select module';
				$this->Template->moduleSelected=false;
			}//else->if($this->Input->post('submit')!='')
			$this->Template->modules=$this->functions->getModules($this->Input->post('module'));
		}
		
	}
}

?>